<?php

/**
 * Description of xmas
 *
 * @author bit0rez
 */

class xmas extends CI_Controller{
	
	var $base_url;
	
        public function __construct() {
                parent::__construct();
		$this->load->database('default');
		$this->load->library("session");
		$this->load->library('Parser');
		$this->load->helper('url');
		$this->load->helper('date');
		
		$this->base_url = base_url();
        }
    
        public function index()
        {
		$data['base_url'] = @$this->base_url;
		$this->parser->parse('xmas/deer.html', $data);
        }
	
	public function twitmin($date = '', $uncheck = '')
	{
		if($date != mdate('%Y-%m-%d')) show_404();
		$this->output->append_output('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//RU" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<title>Tweet`минко v0.1</title>
			<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
			<script type="text/javascript" src="'.$this->base_url.'js/jquery-1.5.1.min.js"></script>
			</head>
			<body>
			');
		if($this->session->userdata('twitrole') != 'twitmin')
			$this->login();
		else
		{
			$this->load->model('webdb');
			$this->output->append_output('<img src="'.$this->base_url.'images/xmas/admin.png" height="100px" alt="*ADMINFACE*" />');
			($uncheck == 'disallow')? $data = $this->webdb->getAllowedTweets() : $data = $this->webdb->getNewTweets();
			$this->output->append_output("<table>");
			foreach($data as $key => $value)
			{
				$this->output->append_output("<tr>");
				$this->output->append_output("<td>".$this->conv($value['name'])."</td>");
				$this->output->append_output("<td>".$this->conv($value['text'])."</td>");
				$this->output->append_output("<td><input type='checkbox' value='".$value['id']."' /></td>");
				$this->output->append_output("</tr>");
			}
			$this->output->append_output("</table>");
			if($uncheck == 'disallow')
			{
				$this->output->append_output("<input type='submit' value='Запретить' />");
				$this->output->append_output("
					<script type='text/javascript'>
						$(document).ready(function(){
							$('input:submit').click(function(){
								var arr = [];
								$.each($('input:checked'),function(){
									arr += ' ';
									arr += $(this).val();
								});
								//if(arr != '')
									$.post('".$this->base_url."xmas/disallow',
									{id: arr},
									function(data){
										if(data.status == 'done')
											document.location = '".$this->base_url."xmas/twitmin/".mdate('%Y-%m-%d')."/disallow';
									},
									'json');
								return false;
							});
						});
					</script>
					");
			}
			else
			{
				$this->output->append_output("<input type='submit' value='Одобрить' />");
				$this->output->append_output("
					<script type='text/javascript'>
						$(document).ready(function(){
							$('input:submit').click(function(){
								var arr = [];
								$.each($('input:checked'),function(){
									arr += ' ';
									arr += $(this).val();
								});
								//if(arr != '')
									$.post('".$this->base_url."xmas/allow',
									{id: arr},
									function(data){
										if(data.status == 'done')
											document.location = '".$this->base_url."xmas/twitmin/".mdate('%Y-%m-%d')."';
									},
									'json');
								return false;
							});
						});
					</script>
					");
			}
		}
	}
	
	public function login()
	{
		$pass = $this->input->post('pass');
		if(!$pass || $pass == '')
		{
			echo "<form action='".$this->base_url."xmas/login' method='post'>";
			echo "<input type=password name=pass />";
			echo "<input type=submit />";
			echo "</form>";
		}
		else if($pass != 'tw33txm45w3bm1n')
		{
			$this->index();
		}
		else
		{
			$this->session->set_userdata(array('twitrole' => 'twitmin'));
			redirect($this->base_url.'xmas/twitmin/'.mdate('%Y-%m-%d'));
		}
		
	}
	
	private function conv($str)
	{
		return iconv('cp1251', 'utf8', $str);
	}
	
	public function allow()
	{
		$this->load->model('webdb');
		$i = 1;
		$data = $this->input->post('id');
		preg_match_all('/[\S]*/', $data, $match);
		while(isset($match[0][$i]))
		{
			$this->webdb->allowTwit($match[0][$i]);
			$i+=2;
		}
		$this->webdb->notNew();
		$this->output->set_output(json_encode(array( 'status' => 'done')));
	}
	
	public function disallow()
	{
		$this->load->model('webdb');
		$i = 1;
		$data = $this->input->post('id');
		preg_match_all('/[\S]*/', $data, $match);
		while(isset($match[0][$i]))
		{
			$this->webdb->disallowTwit($match[0][$i]);
			$i+=2;
		}
		//$this->webdb->notNew();
		$this->output->set_output(json_encode(array( 'status' => 'done')));
	}
	
	public function view()
	{
		$data['base_url'] = @$this->base_url;
		$this->parser->parse('xmas/deer_vvsu.html', $data);
	}
	
	public function getTweet()
	{
		$daurl = 'http://twitter.vvsu.ru:8086/getTweet.php';
		$handle = fopen($daurl, "r");
		if ($handle) {
			while (!feof($handle)) {
				$buffer = fgets($handle, 4096);
				echo $buffer;
			}
			fclose($handle);
		}
	}
}

?>
